<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>资讯管理</el-breadcrumb-item>
        <el-breadcrumb-item>咨询列表</el-breadcrumb-item>
      </el-breadcrumb>

      <el-card>
          <el-row :gutter="24">
              <el-col :span="5">
                  <el-input size="small" clearable placeholder="输入标题" v-model="queryParams.title" /> 
              </el-col>
              <el-col :span="2" >
                  <el-button size="small" type="success" @click="queryPage" icon="el-icon-search" v-if="permissionStrValue.indexOf('advertist:query') !== -1">查询</el-button>
              </el-col>
              <el-col :span="2" >
                  <el-button size="small" type="primary" @click="add" icon="el-icon-plus" v-if="permissionStrValue.indexOf('advertist:add') !== -1">新增</el-button>
              </el-col>
          </el-row>

          <!--放表格-->
        <el-table :data="list" border >
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column label="资讯标题">
                <template slot-scope="scope">
                    <span style="color:#67C23A" size="medium">{{ scope.row.title }}</span>
                </template>
            </el-table-column>
            <el-table-column label="资讯图片">
                <template slot-scope="scope">
                    <el-image
                        style="width: 100px; height: 100px"
                        :src="scope.row.imageUrl"
                ></el-image>
                </template>
            </el-table-column>
            <el-table-column label="资讯日期">
                <template slot-scope="scope">
                    <span style="color:#67C23A" size="medium">{{ scope.row.messageDate}}</span>
                </template>
            </el-table-column>
            <el-table-column label="发布时间">
                <template slot-scope="scope">
                    <span style="color:#67C23A" size="medium">{{ scope.row.createTime}}</span>
                </template>
            </el-table-column>

            <el-table-column label="操作" width="200px">
                 <template slot-scope="scope">
                     <el-button type="warning" size="small" @click="show(scope.row.content)" icon="el-icon-view" v-if="permissionStrValue.indexOf('advertist:info') !== -1">详情</el-button>
                     <el-button type="danger" size="small" @click="removeById(scope.row.id)" icon="el-icon-delete" v-if="permissionStrValue.indexOf('advertist:delete') !== -1">删除</el-button>
                 </template>
            </el-table-column>
        </el-table>

        <!--分页-->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.currPage"
          :page-sizes="[10,20, 30, 40, 50]"
          :page-size="queryParams.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>

        <el-dialog
       title="新增数据"
       :visible.sync="addFlag"
       width="40%"
       @close="clearForm()">
       <el-form :model="messageObj" label-width="100px" ref="messageObj" label-position="left" :rules="messageObjRules">
         <el-form-item label="标题:" prop="title">
           <el-input v-model="messageObj.title" size="small" ></el-input>
         </el-form-item>
         <el-form-item label="资讯内容:" prop="content">
           <el-input v-model="messageObj.content" size="small" type="textarea" :rows="5"></el-input>
         </el-form-item>
         <el-form-item label="资讯日期:" prop="messageDate">
          <div class="block">
                <el-date-picker
                v-model="messageObj.messageDate"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期时间">
                </el-date-picker>
            </div>
         </el-form-item>
         <el-form-item label="资讯封面:" >
           <el-upload
                action="http://121.199.58.229:8180/oss/file/test"
                list-type="picture-card"
                :on-change="beforeProductUpload"
                :auto-upload="false"
                >
                <i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog :visible.sync="imageDialog">
            <img width="100%" :src="messageObj.dialogImageUrl" alt />
            </el-dialog>
         </el-form-item>
       </el-form>
       <span slot="footer" class="dialog-footer">
          <el-button @click="addFlag = false">取 消</el-button>
          <el-button type="primary" @click="addData()">确 定</el-button>
       </span>
     </el-dialog>

     <el-dialog title="资讯内容" :visible.sync="showInfo">
        <el-form >
            <el-form-item label="描述" :label-width="formLabelWidth">
            <el-input v-model="infoContent" autocomplete="off" disabled type="textarea" :rows="5"></el-input>
            </el-form-item>
        <div slot="footer" class="dialog-footer">
            <el-button @click="showInfo = false">取 消</el-button>
        </div>
        </el-form>
</el-dialog>

      </el-card>


    </div>
</template>

<script>
export default {
    name: 'Advertist',
    data() {
        return{
            messageObj: {
                title: '',
                dataValue: '',
                messageDate: '',
                dialogImageUrl: '',
                fileList: [],
                content: ''

            },
            permissionStrValue: '',
            total: 0,
            list: [],
            queryParams: {
                title: '',
                currPage: 1,
                pageSize: 10
            },
            messageObjRules: {
                title: [
                    { required: true, message: '请输入标题', trigger: 'blur' }
               ],
               messageDate: [
                   { required: true, message: '请输入资讯发布时间', trigger: 'blur' }
               ],
               content: [
                   { required: true, message: '请输入资讯内容', trigger: 'blur' }
               ]

            },
            addFlag: false,
            imageDialog: false,
            showInfo: false,
            infoContent: '',
            formLabelWidth: '120px'
        }
    },
    created() {
        this.queryPage();
        //获取权限值
       this.permissionStrValue = JSON.parse(window.sessionStorage.getItem('permissionList')).toString();
        
    },
    methods: {
        async queryPage() {
            const{data: res} = await this.$http.post('/house/message/list-page', JSON.stringify(this.queryParams), {
                 headers: {
                        'Content-Type': 'application/json;charset=UTF-8',
                }
            })
            if(!res.success) {
                return this.$message.error(res.message);
            }
            console.log(res)
            this.list = res.data;
            this.total = res.total;
        },
        add() {
            this.addFlag = true;

        },
        async addData() {
            this.$refs.messageObj.validate(async valid => {
                if(valid) {
                    console.log(this.messageObj.messageDate)
                    var fd = new FormData();
                    fd.append("title", this.messageObj.title);
                    fd.append("content", this.messageObj.content);
                    fd.append("messageDate", this.messageObj.messageDate);
                    if(this.messageObj.fileList.length > 0) {
                        fd.append("image", this.messageObj.fileList[0].raw);
                    }
                    const {data: res} = await this.$http.post('/house/message/add', fd)
                    
                    if(res.success) {
                        this.$message.success("增加成功！");
                        this.addFlag = false;
                        this.clearForm();
                        this.queryPage();
                        this.messageObj.fileList = [];
                    } else{
                        this.$message.error(res.message);
                    }
                }
            })

        },
        beforeProductUpload(file) {
            this.messageObj.fileList.push(file);
        },
        clearForm(){
            this.messageObj.title = '';
            this.messageObj.messageDate = '';
            this.messageObj.content = '';
            this.messageObj.fileList = [];
            this.messageObj.dialogImageUrl = '';

        },
        
        handleSizeChange(newPageSize) {
            this.queryParams.pageSize = newPageSize;
            this.queryPage();
        },
        handleCurrentChange(newPage) {
            this.queryParams.currPage = newPage;
            this.queryPage();

        },

        show(content) {
            this.infoContent = content;
            this.showInfo = true;

        },
        async removeById(id) {
             const confirmResult = await this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).catch(err => {
                return err;
            });
            //当用户确认时，则返回值为字符串：confirm
            //取消时，返回字符串cancel
            if (confirmResult !== 'confirm'){
                return this.$message.info("已取消删除")
            }
            //发送请求
            const{data: res} = await this.$http.delete('/house/message/remove/' + id);
            if(res.success) {
                this.$message.success("删除成功！");
                this.init();
                this.queryPage();
            } else {
                this.$message.error(res.message);
            }
        },
        
        init() {
            this.queryPage.title = '';
            this.queryPage.currPage = 1,
            this.queryPage.pageSize = 10
        }
        
    }
}
</script>